package com.tsinghua.discharge.common;


import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class FileSolution {

    public ServerResponse<String> uploadFile(MultipartFile file,String path){
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String createDate = sdf.format(date);
        fileName = fileName.substring(0,fileName.lastIndexOf(".")) + "-" + createDate + ".csv" ;
        if(!file.isEmpty()){
            File saveFile = new File(path + fileName);
            if(!saveFile.getParentFile().exists()){
                saveFile.getParentFile().mkdirs();
            }
            BufferedOutputStream bos = null;
            try{
                bos = new BufferedOutputStream(new FileOutputStream(saveFile));
                bos.write(file.getBytes());
                bos.flush();
            }catch (Exception e){
                return ServerResponse.createByErrorMessage("文件上传失败"+ e.getMessage());
            }finally {
                try {
                    if(bos != null){
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return ServerResponse.createBySuccess("文件上传成功",fileName);
        }else{
            return ServerResponse.createByErrorMessage("文件为空，上传失败");
        }
    }

    public ServerResponse deleteFile(String filePath){
        try{
            File deleteFile = new File(filePath);
            deleteFile.delete();
            return ServerResponse.createBySuccess("文件删除成功");
        }catch (Exception e){
            return ServerResponse.createByErrorMessage("文件删除失败"+ e.getMessage());
        }
    }
    public ServerResponse<String> uploadXlsxFile(MultipartFile file,String path){
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String createDate = sdf.format(date);
        fileName = fileName.substring(0,fileName.lastIndexOf(".")) + "-" + createDate + ".xlsx" ;
        if(!file.isEmpty()){
            File saveFile = new File(path + fileName);
            if(!saveFile.getParentFile().exists()){
                saveFile.getParentFile().mkdirs();
            }
            BufferedOutputStream bos = null;
            try{
                bos = new BufferedOutputStream(new FileOutputStream(saveFile));
                bos.write(file.getBytes());
                bos.flush();
            }catch (Exception e){
                return ServerResponse.createByErrorMessage("文件上传失败"+ e.getMessage());
            }finally {
                try {
                    if(bos != null){
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return ServerResponse.createBySuccess("文件上传成功",fileName);
        }else{
            return ServerResponse.createByErrorMessage("文件为空，上传失败");
        }
    }
}
